<?php
function get_attachment($artid) {
	global $db, $tablepre, $options;
	$sql = "select * from " .$tablepre."attach where artid = ".intval($artid);
	$query = $db->query($sql);
	while($attach = $db->fetch_array($query)) {
		$attach['value'] = "[img]".$options['url'].'/'.$attach['filename']."[/img]";
		$attachment[] = $attach;
	}
	return $attachment;
}
if($a == 'pagelist') {
	$pur_page = 13;
	$maxshowpage = 4;
	$page = intval(isset($_GET['page']) ? $_GET['page'] : '');
	if($page != '') {
		$start_limit = ($page - 1) * $pur_page;
	} else {
		$start_limit = 0;
		$page = 1;
	}
	$artdb = array();
	$sql = "select id as artid,title,cateid,catename,htmlurl,username,istop,commentnum,tbnum,updatetime from ".$tablepre."articles where cateid = 0 order by id desc";
	$total = $db->rows_count($sql);
	$sql .= " limit ".$start_limit.",".$pur_page;
	$query = $db->query($sql);
	while($article = $db->fetch_array($query)) {
		$article['updatetime'] = gmdate("Y-m-d", $article['updatetime'] + $options['timeoffset']*3600);
		$artdb[] = $article;
	}
	$page = multipage($pur_page, $page, "admin.php?m=page&a=pagelist&", $maxshowpage, $total);
}
if($a == 'pageadd') {
	$sql = "SHOW TABLE STATUS";
	$query = $db->query($sql);
	$table = $db->fetch_array($query);
	$nextid = $table['Auto_increment'];
	$artid = $nextid;
	$attachment = get_attachment($artid);
}
if($a == 'pageedit') {
	$artid = intval($_GET['artid']);
    $sql = "select id as artid,istop,cateid,catename,title,htmlurl,abstract,content,tags,commentnum,tbnum,viewnum,updatetime from ".$tablepre."articles where id=".$artid;
    $query = $db->query($sql);
    $article = $db->fetch_array($query);
	$attachment = get_attachment($artid);
}
if($a == 'pagesave') {
	$artid = intval($_POST['artid']);
    $title = char($_POST['title']);
	$htmlurl = char($_POST['htmlurl']);
 	$content = addslashes($_POST['content']);
	$updatetime = time();
	
	$tourl = 'javascript:history.back();';
	if(!$artid) {
		$sql = "select id from ".$tablepre."articles where htmlurl = '$htmlurl'";
	} elseif($artid) {
		$sql = "select id from ".$tablepre."articles where htmlurl = '$htmlurl' and id <> ".$artid;
	}
	
	if(trim($htmlurl) != '') {
		$query = $db->query($sql);
		if($db->num_rows($query) > 0){
			message('自定义链接名已经存在，请重新选择一个！', $torul);
		}
	}
 	if($title == '') {
		message('标题不能为空', $torul);
	} elseif ($content == '') {
		message('内容不能为空', $torul);
	} else {
		if(!$artid) {
			$sql = "insert into ".$tablepre."articles (istop,cateid,catename,cateurl,username,title,htmlurl,abstract,content,tags,commentnum,tbnum,viewnum,updatetime) values ('0','0','page','0','$username','$title','$htmlurl','null','$content','null','0','0','0','$updatetime')";
 			$db->query($sql);
 			$artid = $db->insert_id();
		} elseif($artid) {
			$sql = "update ".$tablepre."articles set cateurl='$cateurl',username='$username',title='$title',htmlurl='$htmlurl',content='$content',updatetime='$updatetime' where id=".$artid;
			$db->query($sql);
		}
 	}
	navigation_recache();
	message('单页发布成功！', 'admin.php?m=page&a=pagelist');
}
if($a == 'operator') {
	$action = $_POST['action'];
	if($action == 'delete') {
 		$artid = $_POST['artid'];
 		if (empty($_POST['artid']) || !is_array($_POST['artid'])) {
        	message('请选中要操作的单页！', 'admin.php?m=page&a=pagelist');
    	} else {
   			foreach($artid as $articleid=>$value) {
       			$db->query("delete from ".$tablepre."articles where id='".intval($articleid)."'");
   			}
			navigation_recache();
			message('已经删除选中！', 'admin.php?m=page&a=pagelist');
 		} 
	}
}
if($a == 'delattach') {
	$id = intval($_POST['id']);
	$sql = "select path from ".$tablepre."attach where id=".$id;
	$query = $db->query($sql);
	$attach = $db->fetch_array($query);
	$file_path = $attach['path'];
	if(is_file($file_path)) {
		unlink($file_path);
	}
	$db->query("delete from ".$tablepre."attach where id='".intval($id)."'");
}
?>